﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;
using edowork.com.Model;

namespace WebInvoicingsys.system.baseset.sysrole
{
    public partial class usermanagement_edit : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string id,sysaccount, username, mobile, email, stu1, department, job;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                id = Request.Params["id"].ToString();
                sysaccount = Request.Params["sysaccount"].ToString();
                username = Request.Params["username"].ToString();
                mobile = Request.Params["mobile"].ToString();
                email = Request.Params["email"].ToString();
                stu1 = Request.Params["stu1"].ToString();
                department = Request.Params["department"].ToString();
                job = Request.Params["job"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }

        private void bind()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            string sql = "", error_msg = "";

            //构造SQL语句
            sql = @"select a.userid, a.syspassword, a.sysaccount, a.username, a.mobile, a.email, a.stu1, a.remark, a.sysdate, b.jobid, b.jobremark, c.departmentid, c.departmentremark, e.rulegroupid, e.rulegroupremark
                            from sysuser a, job b, department c, sysuserrule d, rulegroup e
                              where a.departmentid=c.departmentid 
                                and a.jobid=b.jobid
                                and a.userid = d.userid 
                                and d.rulegroupid=e.rulegroupid
                                and a.userid='{0}'";

            sql = String.Format(sql, id);

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if ((ds == null) || (ds.Tables.Count <= 0) || (ds.Tables[0].Rows.Count <= 0))
            {
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_sysaccount.Text = dr["sysaccount"].ToString();
                txt_syspassword.Text = "";
                txt_username.Text = dr["username"].ToString();
                txt_mobile.Text = dr["mobile"].ToString();
                txt_email.Text = dr["email"].ToString();
                txt_remark.Text = dr["remark"].ToString();

                uc_from_stu.SetByValue(dr["stu1"].ToString());
                uc_from_department.SetByValue(dr["departmentid"].ToString());
                uc_from_job.SetByValue(dr["jobid"].ToString());
                uc_from_rulegroup.SetByValue(dr["rulegroupid"].ToString());
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParma();

                bind();
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (GetParma() == false)
            {
                return;
            }

            alert_msg = "";

            edowork.com.Model.sysuserInfo sysuserInfo = new sysuserInfo();
            edowork.com.Model.sysuserruleInfo sysuserruleInfo = new sysuserruleInfo();

            sysuserInfo.Sysaccount = txt_sysaccount.Text.Trim();
            sysuserInfo.Username = txt_username.Text.Trim();
            sysuserInfo.Mobile = txt_mobile.Text.Trim();
            sysuserInfo.Email = txt_email.Text.Trim();
            sysuserInfo.Stu1 = uc_from_stu.getValue();
            sysuserInfo.Remark = txt_remark.Text.Trim();
            sysuserInfo.Jobid = uc_from_job.getValue();
            sysuserInfo.Departmentid = uc_from_department.getValue();

            sysuserruleInfo.Rulegroupid = uc_from_rulegroup.getValue();

            sysuserInfo.Syspassword = txt_syspassword.Text.Trim();

            if (String.IsNullOrEmpty(sysuserInfo.Sysaccount))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入登陆账号");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_sysaccount.Focus();

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Username))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请输入用户名称");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                txt_username.Focus();

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Stu1))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择启用状态");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Departmentid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所在部门");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(sysuserInfo.Jobid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所任职务");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            if (String.IsNullOrEmpty(sysuserruleInfo.Rulegroupid))
            {
                alert_msg = String.Format(Scripts.alert_warning, "请选择所属分组");

                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                return;
            }

            //保存数据入数据库
            using (edowork.com.SQL2005.TranUtil tranutil = new TranUtil(Session["connectionString"].ToString()))
            {
                System.Data.SqlClient.SqlTransaction tran = null;

                try
                {
                    tran = tranutil.GetSqlTransaction();
                }
                catch (Exception er)
                {
                    alert_msg = String.Format(Scripts.alert_warning, er.Message);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                edowork.com.SQL2005.Execute exec = new Execute(Session["connectionString"].ToString());

                string sql = "";
                int result = -1;

                //保存入sysuser表
                if(String.IsNullOrEmpty(sysuserInfo.Syspassword))
                {
                    sql = "update sysuser set sysaccount='{1}', username='{2}', mobile='{3}', email='{4}', stu1='{5}', remark='{6}', jobid='{7}', departmentid='{8}' where userid='{0}'";
                    sql = String.Format(sql, id, sysuserInfo.Sysaccount, sysuserInfo.Username, sysuserInfo.Mobile, sysuserInfo.Email, sysuserInfo.Stu1, sysuserInfo.Remark, sysuserInfo.Jobid, sysuserInfo.Departmentid);
                }
                else
                {
                    sql = "update sysuser set sysaccount='{1}', syspassword='{2}', username='{3}', mobile='{4}', email='{5}', stu1='{6}', remark='{7}', jobid='{8}', departmentid='{9}' where userid='{0}'";
                    sql = String.Format(sql, id, sysuserInfo.Sysaccount, SysID.md532(sysuserInfo.Syspassword), sysuserInfo.Username, sysuserInfo.Mobile, sysuserInfo.Email, sysuserInfo.Stu1, sysuserInfo.Remark, sysuserInfo.Jobid, sysuserInfo.Departmentid);
                }
                

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                //保存入sysuserrule表
                sql = "update sysuserrule set rulegroupid='{1}' where userid='{0}'";

                sql = String.Format(sql, id, sysuserruleInfo.Rulegroupid);

                result = exec.ExecSQLQueryInt(sql, ref alert_msg, tran);

                if (result <= 0)
                {
                    tran.Rollback();
                    tranutil.CloseConnection();

                    alert_msg = String.Format(Scripts.alert_warning, alert_msg);

                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);

                    return;
                }

                tran.Commit();
                tranutil.CloseConnection();
            }

            alert_msg = String.Format(Scripts.alert_success, Errormsg.success_Save);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            GetParma();

            string url = "/system/baseset/sysrole/usermanagement_list.aspx?sysaccount={0}&username={1}&mobile={2}&email={3}&stu1={4}&department={5}&job={6}";

            url = String.Format(url, sysaccount, username, mobile, email, stu1, department, job);

            Response.Redirect(url);
        }
    }
}